// pages/card/applydetl/applydetl.js
import Toast from '@vant/weapp/toast/toast';
import Notify from '@vant/weapp/notify/notify';
import Dialog from '@vant/weapp/dialog/dialog';
import { cardApplyStat } from '../../../../common/enums';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: getApp().globalData.CustomBar,
    storeId: getApp().globalData.userInfo.storeId,
    userCardApplyId: null,
    cardInfo: {},
    showDialog: false,
    reason: '',
    fileList: [],
  },
  // 查询卡片申请详情
  qryCardApplyDetl() {
    const param = {
      userCardApplyId: this.data.userCardApplyId
    }
    getApp().callContainer('/api/user/card/queryApply', param, (res) => {
      if (res.data.data) {
        res.data.data.status_label = cardApplyStat.get(res.data.data.status);
        this.setData({ cardInfo: res.data.data });
        if (res.data.data.imgUrlList && res.data.data.imgUrlList.length > 0) {
          const fileList = [];
          for (const imgUrl of res.data.data.imgUrlList) {
            getApp().getTempFilePath(imgUrl).then((res1) => {
              fileList.push({ url: res1 })
              if (res.data.data.imgUrlList.length == fileList.length) {
                this.setData({ fileList: fileList });
              }
            })
          }
        }
      }
    });
  },
  // 审核通过
  onClickToPass() {
    Dialog.confirm({
      message: '确定审核通过吗？',
    }).then(() => {
      const param = {
        userCardApplyId: this.data.userCardApplyId,
        applyStatus: 2,
      }
      getApp().callContainer('/api/store/card/approveApply', param, (res) => {
        Notify({ type: 'success', message: '通过成功！', top: this.data.CustomBar });
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 800);
      })
    }).catch(() => { });
  },
  // 打开审批拒绝原因弹框
  onShowAprlReason() {
    this.setData({ showDialog: true })
  },
  // 打开审批拒绝原因弹框
  onCloseAprlReason() {
    this.setData({ showDialog: false })
  },
  // 审核拒绝
  onClickToRefuse() {
    const param = {
      userCardApplyId: this.data.userCardApplyId,
      applyStatus: 3,
      reason: this.data.reason,
    }
    getApp().callContainer('/api/store/card/approveApply', param, (res) => {
      Notify({ type: 'success', message: '拒绝成功！', top: this.data.CustomBar });
      this.setData({ showDialog: false })
      setTimeout(() => {
        wx.navigateBack({
          delta: 1
        })
      }, 800);
    })
  },
  // 拒绝原因变更
  onChangeToAprlReason(event) {
    this.setData({ reason: event.detail });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.userCardApplyId) {
      this.setData({ userCardApplyId: options.userCardApplyId });
      this.qryCardApplyDetl();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})